package com.mycity.jsonops;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

public class JSONParse {

    public static final String SERVER = "http://titan.cmpe.boun.edu.tr:8087/MyCity/";
    
    public static Object readJSONFromUrl(String urlString) throws IOException, ParseException  {
        BufferedReader reader = null;
        try {
            URL url = new URL(SERVER+urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder buffer = new StringBuilder();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1) {
                buffer.append(chars, 0, read);
            }
            String json = buffer.toString();
            return JSONValue.parseWithException(json);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }  
    
        public static Object readJSONFromFullUrl(String urlString) throws IOException, ParseException  {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder buffer = new StringBuilder();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1) {
                buffer.append(chars, 0, read);
            }
            String json = buffer.toString();
            return JSONValue.parseWithException(json);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }  
    
     public static Boolean readBoolFromUrl(String urlString) throws IOException, ParseException  {
        BufferedReader reader = null;
        try {
            URL url = new URL(SERVER+urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder buffer = new StringBuilder();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1) {
                buffer.append(chars, 0, read);
            }
            String bool = buffer.toString();
            return Boolean.valueOf(bool);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
     
    public static Long readLongFromUrl(String urlString) throws IOException, ParseException  {
        BufferedReader reader = null;
        try {
            URL url = new URL(SERVER+urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder buffer = new StringBuilder();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1) {
                buffer.append(chars, 0, read);
            }
            String s = buffer.toString();
            return Long.valueOf(s);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
}
